<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
       <title>编辑用户</title>
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript">
  	<!--
  	$(document).ready(function(){
  		
		$("#depSel").prepend("<option value='0'>---请选择部门---</option>");
		var insid = $("#insSel").find("option:selected").val();
		var hasDep = ${user.department != null};
		//if(depid==null)
		if(hasDep){
			if(insid != 0){
				$.ajax({
	  	  			type: 'post',
	  	  			url: 'getDepartments.action',
	  	  			data: {insId: insid},
	  	  			dataType:'xml',
	  	  			success: function(xml){
	  	  				depid = ${user.department.id}
	  	  				$(xml).find("department").each(function(){
	  					if($(this).children("id").text() == depid)
	  						$("#depSel").append("<option selected='selected'  value='"+$(this).children("id").text()+"'>"+$(this).children("name").text()+"</option>");
	  					else
	  						$("#depSel").append("<option  value='"+$(this).children("id").text()+"'>"+$(this).children("name").text()+"</option>");
		  				});
	  	  			}
	  	  		});
			}	
		}
		
		$("#insSel").change(function(){
			$("#depSel option").remove();
	  		$("#depSel").prepend("<option value='0'>---请选择部门---</option>");
			var value = $(this).get(0).value;
			//alert(value);
			if(value != 0){
				$.ajax({
	  	  			type: 'post',
	  	  			url: 'getDepartments.action',
	  	  			data: {insId: value},
	  	  			dataType:'xml',
	  	  			success: function(xml){
	  	  				$(xml).find("department").each(function(){
	  	  					$("#depSel").append("<option value='"+$(this).children("id").text()+"'>"+$(this).children("name").text()+"</option>");
	  	  				});
	  	  			}
	  	  		});	
			}
		});
		
		$("#submitBtn").click(function(){
			var userId = $("#userid").val();
	  		var insId = $("#insSel").find("option:selected").val();
	  		var depId = $("#depSel").find("option:selected").val();
	  		var roleId = $("#roleSel").find("option:selected").val();
	  		if(insId == 0){
	  			alert("请选择机构！");
	  			return;
	  		}
	  		if(depId == 0){
	  			alert("请选择部门！");
	  			return;
	  		}
	  		if(roleId == 0){
	  			alert("请选择角色！");
	  			return;
	  		}
	  		//alert("userid="+userId+" insId="+insId+" depId="+depId+" roleId="+roleId);
	  		
	  		$.ajax({
	  			type: 'post',
		  			url: 'allocateUser.action',
		  			data: {id: userId, institutionId: insId, departmentId: depId, roleId: roleId},
		  			success: function(){
		  				alert('successful');
		  				window.location.href='listAllUsers.action';
		  			}
	  		});
		});
		
  	});
  	//-->
  </script>
 
    <link href="images/skin.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
	body{
		margin:0;
		padding:0;
	}
	#top{
		width: 100%;
		height: 63px;
	}
	#left{
		position: absolute;
		left: 0;
		top: 65px;
		width:185px;

	}
	#right{
		position: absolute;
		left: 186px;
		top: 65px;
		width: 1100px;
	}
	</style>
  </head>
  
  
  <body>
  
	
	<div id="top"><jsp:include page="/WEB-INF/page/top.jsp"></jsp:include></div>
	<div id="left"><jsp:include page="/WEB-INF/page/lef.jsp"></jsp:include></div>
	<div id="right">
	
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="17" valign="top" background="images/mail_leftbg.gif"><img src="images/left-top-right.gif" width="17" height="29" /></td>
    <td valign="top" background="images/content-bg.gif"><table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="left_topbg" id="table2">
      <tr>
        <td height="31"><div class="titlebt">编辑用户</div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td valign="middle" background="images/mail_leftbg.gif">&nbsp;</td>
    <td valign="top" bgcolor="#F7F8F9"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td colspan="2" valign="top">&nbsp;</td>
        <td>&nbsp;</td>
        <td valign="top">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" valign="top"><span class="left_bt"></span><br/>
			用户名：${user.name}<br/>
    	<input type="hidden" id="userid" value="${user.id }" />
    	机构：
    	<select id="insSel" name="institutionId" disable="true" onchange="getDep(this);">
			<option  value='0'>---请选择机构---</option>
	    	<c:forEach var="ins" items="${institutions}" >
	    	<c:choose>
	    		<c:when test="${user.institution.id == ins.id }">
	    			<option selected="selected" value="${ins.id }">${ins.name }</option>
	    		</c:when>
	    		<c:otherwise>
	    			<option value="${ins.id }">${ins.name }</option>
	    		</c:otherwise>
	    	</c:choose>
	    	</c:forEach>	
    	</select>
    	<br/>
    	部门：
   		<select name="departmentId" id="depSel">
   			
    	</select>
    	<br/>
    	角色：
    	<select name="roleId" disable="true" id="roleSel">
	    	<option value="0">---请选择角色---</option>
	    	
	    	<c:forEach var="role" items="${roles}" >
	    		<c:choose>
	    		<c:when test="${user.role.id == role.id }">
	    			<option selected="selected" value="${role.id }">${role.name }</option>
	    		</c:when>
	    		<c:otherwise>
	    			<option value="${role.id }">${role.name }</option>
	    		</c:otherwise>
	    	</c:choose>   		
	    	</c:forEach>
    	</select>
    	<br/>
    	<input type="button" id="submitBtn" value="确定" onclick="submit();"><br/>
    	<input type="button" onclick="window.history.go(-1)" value="返回"/>
        </td>
          </tr>
	</div>
  
  </body>
</html>

